﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace chatblog
{
    public partial class addfriend : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                bind1();
                //bind2();
                //bind3();
            }
        }
        private void dealrequest()
        {
            if (this.Request.Params["recid"] == null)
            {
                Response.Redirect(ResolveUrl("~/usercate/chatroom.aspx"));
            }
            if (AliApi.appUserId == "")
            {
                Response.Redirect(ResolveUrl("~/login.aspx?redirecturl=") + this.Request.Url.AbsoluteUri);
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        private void bind1()
        {
            cbusergroup ug = cbusergroup.Find(" cbuserid = '" + AliApi.appUserId + "'", "RECID");
            DropDownList_group.Items.Clear();
            foreach (DataRow item in ug.MyTable.Rows)
            {
                ListItem li = new ListItem (item["cbname"].ToString(),item["cbusergroupid"].ToString());
                DropDownList_group.Items.Add(li);
            }
            if (DropDownList_group.Items.Count > 0)
            {
                DropDownList_group.SelectedIndex = 0;
            }
            if (DropDownList_group.Items.Count == 1)
            {
                Button1_Click(null, EventArgs.Empty);
            }

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            cbuser u = cbuser.Find(" recid = " + this.Request.Params["recid"].ToString());
            string sql = " select * from Vcbusergrouplist where loginuserid = '"
                + AliApi.appUserId + "' and cbuserid = '" + u.cbuserid.ToString() + "' ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show(this.Page, "该用户已经是你的好友,不需要再添加.");
                return;
            }
            cbusergroup ug = cbusergroup.Find(" cbuserid = '" + AliApi.appUserId + "'", "RECID");
            cbusergrouplist ugl = cbusergrouplist.Find(" 1=2 ");
            ugl.New();
            ugl.cbuserid.Value = u.cbuserid.ToString();
            ugl.cbname.Value = zfunction.getnick(u.cbuserid.ToString());
            ugl.cbstatus.Value = "邀请方";
            ugl.cbusergroupid.Value = ug.cbusergroupid.ToString();
            ugl.Insert();
            sql = " select * from Vcbusergrouplist where  cbuserid= '"
                + AliApi.appUserId + "' and loginuserid = '" + u.cbuserid.ToString() + "' ";
            dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            if (dt.Rows.Count < 1)
            {
                ug = cbusergroup.Find(" cbuserid = '" + u.cbuserid.ToString() + "'", "RECID");
                ugl.New();
                ugl.cbuserid.Value = AliApi.appUserId;
                ugl.cbname.Value = zfunction.getnick(AliApi.appUserId);
                ugl.cbstatus.Value = "未验证";
                ugl.cbusergroupid.Value = ug.cbusergroupid.ToString();
                ugl.Insert();
            }
            else
            {
                ugl = cbusergrouplist.Find("  cbuserid= '"
                    + AliApi.appUserId + "' and loginuserid = '" + u.cbuserid.ToString() + "' ");
                ugl.cbstatus.Value="已验证";
                ugl.Modify();
            }
            Response.Redirect(ResolveUrl("~/usercate/chatroom.aspx"));


        }
    }
}
